# Name: [Blog Post Test]
# By: [Wyatt Preul]
# [This will test whether or not an administrator can create a blog post]
# [in the sample blog]
# Maintenance:
#   [9/21/2006] : [WLP] : Created
#

$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..') if $0 == __FILE__
require 'watir'
require 'watir/WindowHelper'
include Watir
require 'test/unit'
require '../watir.config.rb'
require '../resources/functions.rb'


class TC_BlogPost_test < Test::Unit::TestCase
    
    def startup			
      $PostSubject = randomChar(20)
      $PostBody = randomChar(50)
      @@ie = Watir::IE.start($SITE_URL)
    end
  
    def ie
      if defined? @@ie
        @@ie
      else
        startup
      end
    end
      

    def test_01_verifyHomeText
      assert(ie.contains_text(/Community Server/))	
    end
      
    def test_02_verifyLoginLinksExist
      if !(ie.link(:text, 'Sign in').exists?)
        ie.goto($SITE_URL + 'logout.aspx')
        ie.refresh
        ie.goto($SITE_URL)
        ie.refresh
      end
	  end
    
    def test_03_navigateToLogin
      ie.link(:text, 'Sign in').click
      assert(ie.contains_text("Sign in name"))
    end
	  
	  def test_04_populateLoginForm
      ie.text_field(:id, /username$/).value = $ADMIN_USER
      assert(ie.text_field(:id, /username$/).verify_contains($ADMIN_USER))
      
      ie.text_field(:id, /password$/).value = $ADMIN_PASSWORD
      assert(ie.text_field(:id, /password$/).verify_contains($ADMIN_PASSWORD))
	  end
	  
    def test_05_submitLoginForm
      ie.link(:id, /loginButton$/).click
      test_01_verifyHomeText
    end
    
    def test_06_verifyControlPanel
      assert(ie.link(:text, /Control Panel/).exists?)
      ie.link(:text, /Control Panel/).click
      assert(ie.contains_text(/ Announcements/))      
    end
    
    def test_07_verifyControlPanelTabBarLinks
      assert_equal $SITE_URL + 'controlpanel/default.aspx', ie.link(:text, 'Dashboard').href
      assert_equal $SITE_URL + 'controlpanel/blogs', ie.link(:text, 'My Blogs').href
      assert_equal $SITE_URL + 'controlpanel/photos/', ie.link(:text, 'My Photos').href
      assert_equal $SITE_URL + 'controlpanel/files', ie.link(:text, 'My Files').href
      assert_equal $SITE_URL + 'controlpanel/moderation', ie.link(:text, 'Forums Moderation').href
      assert_equal $SITE_URL + 'controlpanel/Setup.aspx', ie.link(:text, 'Administration').href
      assert_equal $SITE_URL + 'controlpanel/Reporting/default.aspx', ie.link(:text, 'Reporting').href
      
    end
    
    def test_08_navigateVerifyMyBlogsDashboard
      ie.link(:text, 'My Blogs').click
      assert(ie.contains_text("My Blog Dashboard"))
      assert(ie.contains_text("Current Blog"))   
    end
    
    def test_09_verifyMyBlogsCommonTasksMenu
      assert(ie.cell(:text, 'Common Tasks').exists?)
      ie.cell(:text, 'Common Tasks').click
    end
    
    def test_10_verifyMyBlogsManageContentMenu
      assert(ie.cell(:text, 'Manage Content').exists?)
      ie.cell(:text, 'Manage Content').click
    end
    
    def test_11_verifyMyBlogsGlobalSettingsMenu
      assert(ie.cell(:text, 'Global Settings').exists?)
      ie.cell(:text, 'Global Settings').click
    end

    def test_12_createBlogPost
      assert(ie.cell(:text, 'Common Tasks').exists?)
      ie.cell(:text, 'Common Tasks').click
      ie.link(:text, 'Write a Blog Post').click
      assert(ie.text_field(:id, /PostSubject$/).exists?)
      ie.text_field(:id, /PostSubject$/).set($PostSubject)
      ie.ie.Document.parentWindow.execScript("tinyMCE.getInstanceById(tinyMCE.getEditorId('mce_editor_0')).execCommand('mceSetContent', false, '" + $PostBody + "')")
    
      ie.link(:text, 'Publish').click
      
      test_09_verifyMyBlogsCommonTasksMenu
      test_10_verifyMyBlogsManageContentMenu
      test_11_verifyMyBlogsGlobalSettingsMenu
      
      assert(ie.link(:text, $PostSubject).exists?)
      
      assert(ie.select_list(:id, /filterPublished$/).exists?)
      ie.select_list(:id, /filterPublished$/).select("Not Published")
      assert(ie.link(:text, 'Apply Filter').exists?)
      ie.link(:text, 'Apply Filter').click
      
      assert(!ie.link(:text, $PostSubject).exists?)
      assert(ie.contains_text('Blog Posts'))
      
    end

	  def test_13_verifyPostOnBlog
      assert(ie.link(:text, 'Exit Control Panel and Return to Site').exists?)
      assert_equal $SITE_URL, ie.link(:text, 'Exit Control Panel and Return to Site').href
      ie.link(:text, 'Exit Control Panel and Return to Site').click
      ie.goto($SITE_URL + 'blogs/')
	  end
    
    def test_14_navigateToPostInAdminPage
      ie.goto($SITE_URL + 'ControlPanel/Blogs/postlist.aspx')
      assert(ie.link(:text, $PostSubject).exists?)
      ie.link(:text, $PostSubject).click
      
      assert(ie.contains_text($PostBody))
    end
    
    def test_99_closeIE
      startClicker("OK" , 1)
      ie.close
    end
end